package com.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.constant.RedisConstant;
import com.entity.PageResult;
import com.entity.QueryPageBean;
import com.entity.Result;
import com.pojo.Setmeal;
import com.service.SetmealService;
import com.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/Setmeal")
public class SetmealController {
   @Reference
   private SetmealService setmealService;
   @Autowired
   private JedisPool jedisPool;
    //上传文件
    @RequestMapping("/upload")
    public Result upload( MultipartFile imgFile) throws IOException {
        Result result=null;
        if (imgFile==null){
            result=new Result(false,"文件不能为空");
            return result;
        }
        long size = imgFile.getSize();
        double v = size * 1.0 / 1024 / 1024;
        if (v>10){
            result=new Result(false,"文件不能超过10M");
            return result;
        }
        String name = imgFile.getOriginalFilename();
        int index = name.lastIndexOf(".");
        String type = name.substring(index);
        if (!".jpg".equalsIgnoreCase(type)){
            result=new Result(false,"文件不是图片类型");
            return result;
        }
        //生成唯一文件名
        String fileName = UUID.randomUUID().toString() + name;
        //开始上传了
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            result=new Result(true, MessageConstant.UPLOAD_SUCCESS,fileName);
            //开始把图片名字存入redis中
            jedisPool.getResource().sadd(RedisConstant.MAXCOLLECTION,fileName);
        } catch (IOException e) {
            result=new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
            e.printStackTrace();
            throw e;
        }
        return result;
    }
    //<!--//查询所有检查组的数据,不使用分页助手-->
   @RequestMapping("/findCheckGroups")
    public Result findCheckGroups(){
       Result checkGroups = null;
       try {
           checkGroups = setmealService.findCheckGroups();
       } catch (Exception e) {
           e.printStackTrace();
           throw e;
       }
       return checkGroups;
   }
    @RequestMapping("/addSetmeal")
    //<!--新增套餐,业务层次还要新增与检查组的中间表-->
    public Result addSetmeal(@RequestBody Setmeal setmeal,@RequestParam("checkgroupIds") Integer[] list){
        Result result = setmealService.addSetmeal(setmeal, list);
        return result;
    }
    //<!--分页查询套餐数据,以及对应的关联检查组数据-->
    @RequestMapping("/findAllSetmeal")
    PageResult findAllSetmeal(@RequestBody QueryPageBean queryPageBean){
        PageResult allSetmeal = setmealService.findAllSetmeal(queryPageBean);
        return allSetmeal;
    };
    //<!--//删除指定套餐,需要删除三张表中的数据-->
    @RequestMapping("/deleteSetmeal")
    public Result deleteSetmeal(int id){
        Result result = setmealService.deleteSetmeal(id);
        return result;
    }
    //修改检查组,需要先把中间表相关数据干掉,再插入中间表数据
    //http://localhost:8080/test/Setmeal/updateSetmeal.do?checkgroupIds=
    @RequestMapping("/updateSetmeal")
    public Result updateSetmeal(@RequestBody Setmeal setmeal,@RequestParam("checkgroupIds") Integer[] list){
        System.out.println("你好呀");
        Result result = setmealService.updateSetmeal(setmeal, list);
        return result;

    }
}
